package com.ckzp.core.util;

/**
 * Created by 51594 on 2022/8/18.
 */
public class RandomUtil {

    public RandomUtil() {
    }

    public static int getInt(int from, int to) {
        return from <= to && from >= 0 ? from + (int) (Math.random() * (double) (to - from + 1)) : -1;
    }

    public static int[] randomCommon(int min, int max, int n) {
        if (n <= max - min + 1 && max >= min) {
            int[] result = new int[n];
            int count = 0;
            while (count < n) {
                int num = (int) (Math.random() * (double) (max - min)) + min;
                boolean flag = true;
                int j = 0;

                while (true) {
                    if (j < n) {
                        if (num != result[j]) {
                            ++j;
                            continue;
                        }
                        flag = false;
                    }
                    if (flag) {
                        result[count] = num;
                        ++count;
                    }
                    break;
                }
            }
            return result;
        } else {
            return new int[0];
        }
    }
}
